﻿using System;
using KlavoNotifications.Storage;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace KlavoNotifications.ViewModels.Notification
{
    public class NotificationViewModel : NotificationObject
    {
        private bool _isPlayers;
        private bool _isVocabularies;
        private bool _isCompetitions;
        private string _players;
        private string _vocabularies;

        private bool _isX2;
        private bool _isX3;
        private bool _isX4;
        private bool _isX5;
        
        private readonly DelegateCommand _okCommand;
        private readonly DelegateCommand _cancelCommand;

        public NotificationViewModel()
        {
            _okCommand = new DelegateCommand(OkCommandExecuted);
            _cancelCommand = new DelegateCommand(CancelCommandExecuted);

            Load();
        }

        public event EventHandler Completed = delegate { };

        public bool IsPlayers
        {
            get { return _isPlayers; }
            set
            {
                _isPlayers = value;
                RaisePropertyChanged(() => IsPlayers);
            }
        }

        public bool IsVocabularies
        {
            get { return _isVocabularies; }
            set
            {
                _isVocabularies = value;
                RaisePropertyChanged(() => IsVocabularies);
            }
        }

        public bool IsCompetitions
        {
            get { return _isCompetitions; }
            set
            {
                _isCompetitions = value;
                RaisePropertyChanged(() => IsCompetitions);
            }
        }

        public string Players
        {
            get { return _players; }
            set
            {
                _players = value;
                RaisePropertyChanged(() => Players);
            }
        }

        public string Vocabularies
        {
            get { return _vocabularies; }
            set
            {
                _vocabularies = value;
                RaisePropertyChanged(() => Vocabularies);
            }
        }

        public DelegateCommand OkCommand
        {
            get { return _okCommand; }
        }

        public DelegateCommand CancelCommand
        {
            get { return _cancelCommand; }
        }

        public bool IsX2
        {
            get { return _isX2; }
            set
            {
                _isX2 = value;
                RaisePropertyChanged(() => IsX2);
            }
        }

        public bool IsX3
        {
            get { return _isX3; }
            set
            {
                _isX3 = value;
                RaisePropertyChanged(() => IsX3);
            }
        }

        public bool IsX4
        {
            get { return _isX4; }
            set
            {
                _isX4 = value;
                RaisePropertyChanged(() => IsX4);
            }
        }

        public bool IsX5
        {
            get { return _isX5; }
            set
            {
                _isX5 = value;
                RaisePropertyChanged(() => IsX5);
            }
        }

        private void CancelCommandExecuted()
        {            
            OnCompleted(null);
        }

        private void OkCommandExecuted()
        {
            NotificationStorageData data = new NotificationStorageData();

            data.IsCompetitions = _isCompetitions;
            data.IsPlayers = _isPlayers;
            data.IsVocabularies = _isVocabularies;
            data.IsX2 = _isX2;
            data.IsX3 = _isX3;
            data.IsX4 = _isX4;
            data.IsX5 = _isX5;

            string[] splitPlayers = Players.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
            foreach(var xPlayer in splitPlayers)
                data.Players.Add(xPlayer.Trim());

            string[] splitVocs = Vocabularies.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var xVoc in splitVocs)
                data.Vocabularies.Add(xVoc.Trim());

            NotificationStorage storage = App.Get<NotificationStorage>();
            storage.Save(data);

            OnCompleted(null);
        }

        private void OnCompleted(EventArgs e)
        {
            EventHandler handler = Completed;
            if (handler != null) handler(this, e);
        }

        private void Load()
        {
            NotificationStorage storage = App.Get<NotificationStorage>();

            NotificationStorageData data = storage.Data;

            Players = String.Join(Environment.NewLine, data.Players);
            Vocabularies = String.Join(Environment.NewLine, data.Vocabularies);

            IsPlayers = data.IsPlayers;
            IsVocabularies = data.IsVocabularies;
            IsCompetitions = data.IsCompetitions;
            IsX2 = data.IsX2;
            IsX3 = data.IsX3;
            IsX4 = data.IsX4;
            IsX5 = data.IsX5;
        }
    }
}